package com.blb.ssm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.blb.ssm.dao.SupplierMapper;
import com.blb.ssm.entity.Supplier;
import com.blb.ssm.service.SupplierService;

@Service
public class SupplierServiceImpl implements SupplierService {
	
	@Autowired
	private SupplierMapper supplierMapper;
	
	/**
	 * 分页、条件查询
	 */
	@Transactional(rollbackFor=Exception.class)
	@Override
	public List<Supplier> getlist(String scom, String stel,int page,int limit) throws Exception {
		int start=(page-1)*limit;
		return supplierMapper.getlist(scom,stel,start,limit);
	}
	/**
	 * 获取数据总条数
	 */
	@Transactional(rollbackFor=Exception.class)
	@Override
	public int getCount(String scom, String stel) throws Exception {
		return supplierMapper.getcount(scom,stel);
	}
	
	/**
	 * 删除
	 */
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void delSupplier(String sid)throws Exception {
		supplierMapper.delSupplier(sid);
	}
	/**
	 * 新增：判断数据库中是否有相同的名称（有就不添加），判断数据是否已经有默认选项（有就将数据库的默认选项取消）
	 */
	@Transactional(rollbackFor=Exception.class)
	@Override
	public String addSupplier(Supplier supplier)throws Exception {
		Supplier supplier2=supplierMapper.checkSdef();
		if(supplier2 !=null){
			//只能有一个默认选项
			supplierMapper.updateSdef(supplier2.getSid());
		}
		Supplier supplier1=supplierMapper.selectSupplierByScom(supplier.getScom());
		if(supplier1 !=null){
			return "该名称已经存在!";
		}else{
			supplierMapper.addSupplier(supplier);
			return "添加供应商成功!";
		}
	}
	/**
	 * 编辑（其他判读条件与新增一样）
	 */
	@Transactional(rollbackFor=Exception.class)
	@Override
	public String updateSupplier(Supplier supplier)throws Exception {
		Supplier supplier2=supplierMapper.checkSdef();
		if(supplier2 !=null){
			//只能有一个默认选项
			supplierMapper.updateSdef(supplier2.getSid());
		}
		Supplier supplier1=supplierMapper.selectSupplierByScom(supplier.getScom());
		String sid = supplierMapper.selectSupplierBySID(supplier.getScom());
		if(supplier1 !=null && !sid.equals(supplier.getSid())){
			return "该名称已经存在!";
		}else{
			supplierMapper.updateSupplier(supplier);
			return "供应商编辑成功!";
		}
	}
}
